/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.swirrel.installer;

import java.awt.Component;
import java.awt.event.ComponentListener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.swirrel.annotation.ComponentHidden;
import org.swirrel.annotation.ComponentMoved;
import org.swirrel.annotation.ComponentResized;
import org.swirrel.annotation.ComponentShown;
import org.swirrel.listener.SwirrelComponentListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class ComponentListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {
    public ComponentListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(ComponentHidden.class,
                ComponentMoved.class, ComponentResized.class, ComponentShown.class),
                Arrays.<Class<?>>asList(Component.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
       ComponentListener[] componentListeners = ((Component) obj).getComponentListeners();
        for (ComponentListener cl : componentListeners) {
            if (cl instanceof SwirrelComponentListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, java.util.List<MethodCall> methodCalls) throws IllegalAccessException {
        Object obj = field.get(comp);
        ComponentListener cl = new SwirrelComponentListener(methodCalls.get(0),
                methodCalls.get(1), methodCalls.get(2), methodCalls.get(3));
         ((Component) obj).addComponentListener(cl);
    }
}
